<extend name="Layout:form"/>
<block name="title"><title>添加类型</title></block>
<block name="subnav">
    <a class="add fb" href="__CONTROLLER__/add"><em>添加模型</em></a>　    <a href="__CONTROLLER__/index" class="on"><em>模型管理</em></a>
</block>
<block name="main">
    <form action="__CONTROLLER__/add" method="post" name="myform" id="myform">
        <table cellpadding="2" cellspacing="1" class="table_form" width="100%">
            <tbody>
                <tr>
                    <th width="100">模型名称：</th>
                    <td><input type="text" name="model[name]" id="model_name" size="30" class="input-text"></td>
                </tr>
                <tr>
                    <th width="100">模型表名：</th>
                    <td><input type="text" name="model[tablename]" id="tablename" size="30" class="input-text"></td>
                </tr>
                <tr>
                    <th width="100">模型菜单名称：</th>
                    <td><input type="text" name="model[menuname]" id="menuname" size="30" placeholder="默认为模型名称" class="input-text"></td>
                </tr>
                <tr>
                    <th><strong>模型类别：</strong></th>
                    <td class="model_type">
                        <label><input type="radio" name="model[type]" value="0" checked> 独立模型</label>
                        <label><input type="radio" name="model[type]" value="1"> 父模型</label>
                        <label><input type="radio" name="model[type]" value="2"> 子模型</label>
                    </td>
                </tr>
                <tr class="parent_model" style="display: none;">
                    <th><strong>父模型：</strong></th>
                    <td>
                        <select id="parentid" name="model[parentid]">
                            <option value="0">选择父模型</option>
                            <?php foreach ($models as $key => $model) { ?>
                            <option value="<?php echo $model['id']; ?>"><?php echo $model['name']; ?></option>
                            <?php } ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>描述：</th>
                    <td><textarea name="model[description]" id="description" cols="50" rows="6"></textarea></td>
                </tr>
                <tr>
                    <th></th>
                    <td><input type="submit" name="dosubmit" id="dosubmit" class="button" value=" 提交 "></td>
                </tr>
            </tbody>
        </table>
    </form>
</block>
<block name="footer">
    <script type="text/javascript">
        $(function() {
            $.formValidator.initConfig({formid:"myform",autotip:true,onerror:function(msg,obj){window.top.art.dialog({content:msg,lock:true}, function(){this.close();$(obj).focus();})}});
            $("#model_name").formValidator({onshow:"请输入分类名称",onfocus:"请输入模型名称"}).inputValidator({min:1,onerror:"请输入模型名称"}).ajaxValidator({type : "get",url : "__CONTROLLER__/public_check_name?field=name",data :"",datatype : "html",async:'false',success : function(data){  if( data=="1" ){return true;}else{return false;}},buttons: $("#dosubmit"),onerror : "名称已存在。",onwait : "正在连接，请稍候。"});
            $("#tablename").formValidator({onshow:"请输入分类名称",onfocus:"请输入表名"}).inputValidator({min:1,onerror:"请输入表名"}).ajaxValidator({type : "get",url : "__CONTROLLER__/public_check_name?field=tablename",data :"",datatype : "html",async:'false',success : function(data){  if( data == "1" ){return true;}else{return false;}},buttons: $("#dosubmit"),onerror : "名称已存在。",onwait : "正在连接，请稍候。"});

            $("#parentid").formValidator({onshow:"请选择父模型",onfocus:"请选择父模型"}).functionValidator({
                fun: function(value, _this) {
                    $model_type = $('.model_type input:checked').val();
                    if ($model_type == 2 && value <= 0) {
                        return false;
                    }
                    return true;
                },
                onerror: '请选择父模型！'
            });

            $('.model_type input').change(function() {
                if ($(this).val() == 2) {
                    $('.parent_model').show();
                } else {
                    $('.parent_model').hide();
                }
            });
        });
    </script>
</block>